package com.zx.designpattern;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.midcent.networklib.manager.HttpRequestNetManager;
import com.zx.designpattern.view.activity.Interface4AllActivity;
import com.zx.designpattern.view.activity.MVPActivity;
import com.zx.designpattern.view.activity.TestHttpActivity;
import com.zx.designpattern.view.activity.VolleyTestActivity;
import com.zx.designpattern.base.BaseActivity;
import com.zx.designpattern.model.http.URLUtils;
import com.zx.designpattern.utils.interfaceforall.FunctionManager;
import com.zx.designpattern.utils.interfaceforall.FunctionNoParamNoResult;
import com.zx.designpattern.utils.interfaceforall.FunctionWithParamAndResult;
import com.zx.designpattern.utils.interfaceforall.FunctionWithParamOnly;
import com.zx.designpattern.utils.interfaceforall.FunctionWithResultOnly;
import com.zx.designpattern.presenter.BasePresenter;

import java.util.HashMap;

public class MainActivity extends BaseActivity {

    //    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        HttpRequestNetManager.setServerHost(URLUtils.BASE_URL);
        addInterface();
        HashMap MAP = new HashMap();
        for (int i = 0; i < 10; i++) {
            MAP.put(i, i + "xxx");
        }
//        Logger.d(MAP);
//        Logger.d(SET);
//        Logger.d(LIST);
//        Logger.d(ARRAY);
//        Logger.json("[{'1':'2'},{'1':'2'},{'1':'2'},{'1':'2'},{'1':'2'},{'1':'2'},{'1':'2'},{'1':'2'}]");
//        Logger.xml("<xml><xml><xml></xml></xml><xml></xml><xml><xml></xml></xml><xml></xml></xml>");
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int getRootViewId() {
        return R.layout.activity_main;
    }

    /**
     * 接口名称：最好使用当前类名，保证唯一性！
     */
    public String INTERFACE_NAME = MainActivity.this.getClass().getName();

    /**
     * 增加接口
     */
    protected FunctionManager addInterface() {
        //链式增加接口
        return FunctionManager.getInstance().addFunctionNoParamNoResult(new FunctionNoParamNoResult(INTERFACE_NAME) {
            @Override
            public void function() {
//                Toast.makeText(MainActivity.this, "addInterfaceForFragment 1 你好！没有返回值，没有参数的接口！", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(MainActivity.this, Interface4AllActivity.class);
                startActivity(intent);
            }
        }).addFunctionWithParamAndResult(new FunctionWithParamAndResult<String, Integer>(INTERFACE_NAME) {
            @Override
            public String function(Integer param) {
                Intent intent = new Intent(MainActivity.this, MVPActivity.class);
                startActivity(intent);
                return (param + 1) + "----你好！有返回值，有参数的接口！";
            }
        }).addFunctionWithParamOnly(new FunctionWithParamOnly<String>(INTERFACE_NAME) {
            @Override
            public void function(String param) {
                Toast.makeText(MainActivity.this, "addInterfaceForFragment 3 param is " + param, Toast.LENGTH_SHORT).show();

            }
        }).addFunctionWithResultOnly(new FunctionWithResultOnly<String>(INTERFACE_NAME) {
            @Override
            public String function() {
                return "你好！只有返回值，没有参数的接口";
            }
        });

    }


    public void invoke(View view) {
//触发已经增加的接口
        FunctionManager.getInstance().invokeFunctionNoParamNoResult(INTERFACE_NAME);
    }

    public void invoke2(View view) {
        String str = FunctionManager.getInstance().invokeFunctionWithParamAndResult(INTERFACE_NAME, String.class, 10);
//        Toast.makeText(MainActivity.this, "addInterfaceForFragment 2 return is " + str, Toast.LENGTH_SHORT).show();

    }

    public void invoke3(View view) {
        FunctionManager.getInstance().invokeFunctionWithParamOnly(INTERFACE_NAME, " 你好！只有参数没有返回值的接口！");

    }

    public void invoke4(View view) {
//        String str = FunctionManager.getInstance().invokeFunctionWithResultOnly(INTERFACE_NAME, String.class);
//        Toast.makeText(MainActivity.this, "addInterfaceForFragment 4 return is " + str, Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(MainActivity.this, VolleyTestActivity.class);
        startActivity(intent);
    }

    public void TestHttpActivity(View view) {
        Intent intent = new Intent(MainActivity.this, TestHttpActivity.class);
        startActivity(intent);
    }

    //移除接口
    @Override
    protected void removeInterface() {
        FunctionManager.getInstance().clear(INTERFACE_NAME);
    }

    @Override
    public void showView(int code, Object data) {

    }
}
